layui.config({
    base: '../res/static/js/util/' //你存放新模块的目录，注意，不是layui的模块目录
}).use(['jquery','cookie'],function(){
	var $ = layui.jquery;
	var cookie = layui.cookie;
	
	//加载登录用户信息
	var user = sessionStorage.getItem("user");
	if(user==null){//如果用户没有登录，跳转到登录页面
		location.href = "login.html";
	}
	user = JSON.parse(user);//将sessionStorage中存储的json转换成字符串
	//console.log(user)
	$(".login").html('欢迎，<span style="font-size:20px ">'+user.nickname+'</span>&nbsp;&nbsp;&nbsp;&nbsp;<a id="logout" href="javascript:;">安全退出</a>');
	
	//退出按钮点击事件
	$("#logout").click(function(){
		$.cookie('token',null);//清除cookie中的token
		sessionStorage.removeItem("user");//删除session
		location.href="login.html";//跳转到登录页
	});
	
	//加载购物车商品种数
	$.post(
		"../../getCount.do?user_id="+user.user_id,
		function(res){
			$(".sp-cart span").html(res);
		},"json"
	);


 //加载首页商品分类
 $.post(      
	"../../findAllcategory.do",
	function(res){
		$(res.data).each(function(){
			//创建li标签
			var  li = $('<li class="nav-item"><div class="title">'+this.category_name+'</div> <i class="layui-icon layui-icon-right"></i></li>');
			//将li标签追加到UI中
			$("#Category01").append($(li));
	});
	},"json"
);



//更多推荐
$.post(      
	"../../findAllrecommendgoods.do",
	function(res){
		$(res.data).each(function(){
			//创建div标签
			var  div = $('<div class="list-item"><a href="details.html?goods_id='+this.goods_id+'"><img src="/uploads/'+this.picture+'" width="190px" height="190px"></a><p style="text-align:center">'+this.goods_name+'</p><span>￥'+this.price+'</span></div>');
			//将div标签追加到div中
			$("#tuiJian").append($(div));
	});
	},"json"
);



	//加载今日必抢
	$.post(
		"../../Goods/findToday.do",
		function(res){
			var html = '<div class="item-box">';
			$(res).each(function(index){
				var goods_name = this.goods_name.length>8?this.goods_name.substr(0,12)+"...":this.goods_name;
				var delPrice = (this.price*1.6).toFixed(2);//保留2位小数
				html+='<div class="item"><a href="details.html?goods_id='+this.goods_id+'"><img src="/uploads/'+this.picture+'"></a><div class="title">'+goods_name+'</div><div class="price"><span>￥'+this.price+'</span><del>￥'+delPrice+'</del></div></div>';
				if(index%4==3){
					html+='</div>';
					html+='<div class="item-box">';
				}
			});
			html+='</div>';
			$("#today").append($(html));
		},"json"
	);
});


